package com.web.surf.sample.eshop.faces.managed.beans;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.web.surf.sample.eshop.dto.interfaces.IProperty;
import com.web.surf.sample.eshop.faces.core.AbstractBean;

/**
 * Order substance 
 * @author SavenkoNV
 */
public class OrderBean extends AbstractBean implements Serializable {

	private static final long serialVersionUID = -6511467340857264367L;
	private AddressBean addressBean;
	private Map<GoodsBean, Integer> basket = new HashMap<GoodsBean, Integer>();
		
	/**
	 * Order properties 
	 * @author SavenkoNV
	 */
	public enum OrderProperties implements IProperty {

		ORDER_ID("order_id"),
		ORDER_DATE("order_date"),
		DELIVERY_DATE("delivery_date"),
		TOTAL_NUMBER("total_number"),
		TOTAL_PRICE("total_price"),
		
		GOODS_ID("goods_id"),
		GOODS_NUMBER("number");	

		private String databaseName;

		OrderProperties(String databaseName) {
			this.setDatabaseName(databaseName);
		}

		public void setDatabaseName(String databaseName) {
			this.databaseName = databaseName;
		}

		public String getDatabaseName() {
			return databaseName;
		}
	}	
	
	public OrderBean() {
		setOrderID(generateID());
	}

	public void clear() {
		setOrderID(generateID());
		addressBean.clear();
		basket.clear();
		setOrderDate(null);
		setDeliveryDate(null);
		setTotalGoodsNumber(0);
		setTotalPrice(0);
		basket.clear();

	}
	
	public boolean equals(Object o) {
		if ((o instanceof OrderBean)&&(((OrderBean)o).getOrderID().equals(this.getOrderID()))) {
			return true;
		} else {
			return false;		
		}

	}
	
	public int hashCode() {
		return getOrderID().hashCode();

	}


	public void setBasket(Map<GoodsBean, Integer> basket) {
		this.basket = basket;
	}
	
	public Map<GoodsBean, Integer> getBasket() {
		return basket;
	}
	
	public AddressBean getAddressBean() {
		return addressBean;
	}
	//Spring setter
	public void setAddressBean(AddressBean addressBean) {
		this.addressBean = addressBean;
	}
	//Property getters 
	public String getOrderID() {
		return getProperty(OrderProperties.ORDER_ID, String.class);
	}	
	
	public Date getOrderDate() {
		return getProperty(OrderProperties.ORDER_DATE, Date.class);
	}
	
	public Date getDeliveryDate() {
		return getProperty(OrderProperties.DELIVERY_DATE, Date.class);
	}
	
	public int getTotalGoodsNumber() {
		return getProperty(OrderProperties.TOTAL_NUMBER, Integer.class);
	}

	public double getTotalPrice() {
		return getProperty(OrderProperties.TOTAL_PRICE, Double.class);
	}	
	//Property setters
	public void setOrderID(String orderID) {
		addProperty(OrderProperties.ORDER_ID, orderID);
	}
	
	public void setOrderDate(Date orderDate) {
		addProperty(OrderProperties.ORDER_DATE, orderDate);
	}
	
	public void setDeliveryDate(Date deliveryDate) {
		addProperty(OrderProperties.DELIVERY_DATE, deliveryDate);
	}
	
	public void setTotalGoodsNumber(int totalGoodsNumber) {
		addProperty(OrderProperties.TOTAL_NUMBER, totalGoodsNumber);
	}

	public void setTotalPrice(double totalPrice) {
		addProperty(OrderProperties.TOTAL_PRICE, totalPrice);
	}

}